package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.SignPlanVersionDetail;
import cn.com.cifi.mars.mapper.mars.SignPlanVersionDetailCopyMapper;
import cn.com.cifi.mars.mapper.mars.SignPlanVersionDetailMapper;
import cn.com.cifi.mars.service.SignPlanVersionDetailCopyService;
import cn.com.cifi.mars.service.SysLogService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 签约计划版本详情快照表 服务实现类
 * </p>
 *
 * @author jyx
 * @since 2020-03-02
 */
@Service
public class SignPlanVersionDetailServiceCopyImpl extends ServiceImpl<SignPlanVersionDetailCopyMapper, SignPlanVersionDetail> implements SignPlanVersionDetailCopyService {

    @Autowired
    private SignPlanVersionDetailMapper signPlanVersionDetailMapper;
    @Autowired
    private SignPlanVersionDetailCopyMapper signPlanVersionDetailCopyMapper;
    @Autowired
    private SysLogService sysLogService;

    /**
     * 复制指定版本的签约计划详情
     * @param versionId 版本id
     */
    @Override
    public void copyDetail(String versionId) {
        try {
            List<SignPlanVersionDetail> signPlanVersionDetailList = signPlanVersionDetailMapper.selectByVersionId(versionId, IsDeleteEnum.NO.getKey());
            signPlanVersionDetailCopyMapper.insertList(signPlanVersionDetailList);
            saveLog("签约计划","对销管倒挤后的签约详情做快照","版本id:"+ versionId+"快照成功");
        }catch (Exception e){
            saveLog("签约计划","对销管倒挤后的签约详情做快照","版本id:"+ versionId+"快照失败，原因:" + e.toString());
        }
    }

    /**
     * 对指定版本的签约计划详情做对比
     * @param versionId 版本id
     * @return true:无差异,false:有差异
     */
    @Override
    public boolean dataCompare(String versionId) {
        //签约计划详情表数据集合
        List<SignPlanVersionDetail> spvdList = signPlanVersionDetailMapper.selectByVersionId(versionId, IsDeleteEnum.NO.getKey());
        //签约计划详情快照表数据集合
        List<SignPlanVersionDetail> spvdCopyList = signPlanVersionDetailCopyMapper.selectByVersionId(versionId, IsDeleteEnum.NO.getKey());
        if(CollectionUtils.isEmpty(spvdCopyList)){
            return true;
        }
        for (SignPlanVersionDetail spvdCopy : spvdCopyList){
            String groupId = spvdCopy.getGroupId();
            String productCode = spvdCopy.getProductCode();
            String businessType = spvdCopy.getBusinessType();
            String freeType = spvdCopy.getFreeType();
            String proType = spvdCopy.getProType();
            //签约时间
            LocalDateTime signPlanTime = spvdCopy.getSignPlanTime();
            List<SignPlanVersionDetail> list = spvdList.stream().filter(s ->
                    signPlanTime.equals(s.getSignPlanTime()) && groupId.equals(s.getGroupId())
                    && productCode.equals(s.getProductCode()) && businessType.equals(spvdCopy.getBusinessType())
                    && freeType.equals(s.getFreeType()) && proType.equals(s.getProType()))
                    .collect(Collectors.toList());
            //若不存在,表示数据不一致
            if(CollectionUtils.isEmpty(list)){
                return false;
            }
            BigDecimal earlySignSumPrice = list.get(0).getEarlySignSumPrice();
            BigDecimal supplySignSumPrice = list.get(0).getSupplySignSumPrice();
            BigDecimal earlySignSumPriceCopy = spvdCopy.getEarlySignSumPrice();
            BigDecimal supplySignSumPriceCopy = spvdCopy.getSupplySignSumPrice();
            if(earlySignSumPrice.compareTo(earlySignSumPriceCopy) != 0){
                return false;
            }
            if(supplySignSumPrice.compareTo(supplySignSumPriceCopy) != 0){
                return false;
            }
        }

        return true;
    }

    private boolean saveLog(String module,String title,String content) {
        SysLogBO bo = new SysLogBO();
        bo.setEvent(LogEventEnum.PRESERVATION);
        bo.setSource(LogSrcEnum.SYS);
        bo.setModule(module);
        bo.setTitle(title);
        bo.setContent(content);
        bo.setType(LogTypeEnum.ONE.getKey());
        bo.setIsSuccess(IsSuccessEnum.SUCCESS.getKey());
        return sysLogService.add(bo,Constants.SYS);
    }
}
